/*
 * File: ftkbutton.cpp
 * Author:  Jiao JinXing <jiaojinxing1987@gmail.com>
 * Brief:   ftk button.
 *
 * Copyright (c) 2010 - 2011  Jiao JinXing <jiaojinxing1987@gmail.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * Jiao JinXing <jiaojinxing1987@gmail.com> created
 *
 */

#include "ftkbutton.h"
#include "mainwindow.h"

#include <QtGui>

FtkButton::FtkButton(WidgetTree *tree, QDomElement &element, QTreeWidgetItem *item, QWidget *parent) :
        QPushButton(parent)
{
    this->item = item;
    this->tree = tree;
    this->element = element;

    if (!element.hasAttribute("w")) {
        if (curDesignWindow->widgetSizeRelatively) {
            element.setAttribute("w", "$pw/8");
        } else {
            element.setAttribute("w", "80");
        }
    }

    if (!element.hasAttribute("h")) {
        if (curDesignWindow->widgetSizeRelatively) {
            element.setAttribute("h", "$ph/16");
        } else {
            element.setAttribute("h", "30");
        }
    }

    if (!element.hasAttribute("value")) {
        element.setAttribute("value", "button");
    }

    int x, y, w, h;
    FtkXul xul;
    x = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("x").toAscii().data()));
    y = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("y").toAscii().data()));
    w = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("w").toAscii().data()));
    h = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("h").toAscii().data()));
    setGeometry(x, y, w, h);

    setText(element.attribute("value"));
    show();

    normalStyleSheet = this->styleSheet();

    QPalette pal = palette();
    pal.setBrush(QPalette::Base, QPixmap(":/images/background.png"));
    pal.setColor(QPalette::HighlightedText, Qt::red);
    setPalette(pal);
}

FtkButton::~FtkButton()
{
    if (curDesignWindow->widgetsRemoving)
        tree->deleteItemElement(item, element);
}

void FtkButton::mousePressEvent(QMouseEvent *event)
{
    if (!MainWindow::ctrlPress) {
        IGNORE_CTRL:
        if (event->button() == Qt::LeftButton) {

            tree->setCurrentItem(item);

            if (resizeHandle().contains(event->pos())) {
                changeSize = true;
                dragPosition = frameGeometry().bottomRight() - event->globalPos();
            } else {
                changeSize = false;
                dragPosition = event->globalPos() - frameGeometry().topLeft();
            }
        }
    } else {
        if (curDesignWindow->setMainWidget(this) == false) {
            goto IGNORE_CTRL;
        }
    }
    event->accept();
}

static const int resizeHandleWidth = 6;

void FtkButton::mouseMoveEvent(QMouseEvent *event)
{
    if (!MainWindow::ctrlPress) {
        if (event->buttons() == Qt::LeftButton) {
            if (changeSize) {
                QPoint br = event->globalPos() + dragPosition;

                setGeometry(QRect(geometry().topLeft(), br));

                if (curDesignWindow->widgetSizeRelatively) {
                    QString w;
                    w.sprintf("$pw*%d/%d", width(), parentWidget()->width());
                    element.setAttribute("w", w);
                } else {
                    element.setAttribute("w", width());
                }

                if (curDesignWindow->widgetSizeRelatively) {
                    QString h;
                    h.sprintf("$ph*%d/%d", height(), parentWidget()->height());
                    element.setAttribute("h", h);
                } else {
                    element.setAttribute("h", height());
                }

                tree->somethingChanged();

            } else {
                QPoint newPos = event->globalPos() - dragPosition;

                if (newPos.x() < 0) {
                    newPos.setX(0);
                }
                if (newPos.y() < 0) {
                    newPos.setY(0);
                }
                if (newPos.x() + width() >= parentWidget()->width()) {
                    newPos.setX(parentWidget()->width() - width() - 1);
                }
                if (newPos.y() + height() >= parentWidget()->height()) {
                    newPos.setY(parentWidget()->height() - height()- 1);
                }
                move(newPos);

                if (curDesignWindow->widgetSizeRelatively) {
                    QString pos;
                    pos.sprintf("$pw*%d/%d", x(), parentWidget()->width());
                    element.setAttribute("x", pos);
                    pos.sprintf("$ph*%d/%d", y(), parentWidget()->height());
                    element.setAttribute("y", pos);
                } else {
                    element.setAttribute("x", x());
                    element.setAttribute("y", y());
                }

                tree->somethingChanged();
            }
        }
    }

    event->accept();
}

QRect FtkButton::resizeHandle() const
{
    QPoint br = geometry().bottomRight() - geometry().topLeft() - QPoint(1, 1);
    return QRect(br - QPoint(resizeHandleWidth, resizeHandleWidth), br);
}

void FtkButton::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);

    if (curDesignWindow->mainWidget == this) {
        QPainter painter(this);

        QPen pen;
        pen.setColor(QColor(0,0,255));
        QBrush brush(QColor(0,0,255));

        painter.setPen(pen);
        painter.setBrush(brush);

        painter.drawRect(resizeHandle());
    }
}
